/* ----------------------------------------------------------------------------
* File: file_logger.h
*
* Desc: CFileLogger class declaration for file logging
*
* Created: 04/03/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#ifndef _FILE_LOGGER_H_
#define _FILE_LOGGER_H_

#include <stdio.h>
#include <exception>
#include <stdarg.h>
#include "common.h"


/************************************************************************/
/*	CFileLogger class to log into the file								*/
/************************************************************************/
class CFileLogger
{
public:
	CFileLogger() throw(): m_fp(NULL)
	{
	}

	CFileLogger(const char *fileName) throw(std::exception)
	{
		m_fp = fopen(fileName, "at");
		if (m_fp == NULL)
			throw std::exception();
	}

	~CFileLogger()
	{
		if (m_fp != NULL)
		{
			fclose(m_fp);
			m_fp = NULL;
		}
	}

	int Open(const char *fileName)
	{
		if (m_fp == NULL)
		{
			m_fp = fopen(fileName, "at");
			if (m_fp == NULL)
				return BOX_SERVER_ERROR;
		}
		return BOX_SERVER_OK;
	}

	virtual void Print(const char *msg, ...)
	{
		if (m_fp != NULL)
		{
			va_list argptr;

			va_start(argptr, msg);
			vfprintf(m_fp, msg, argptr);
			fflush(m_fp);
			va_end(argptr);
		}
	}

	FILE* GetFileDescriptor() { return m_fp; }

private:
	FILE *m_fp;
};


#endif //_FILE_LOGGER_H_